//
//  RegisteredViewController.m
//  Only Love
//
//  Created by 郭健 on 16/5/11.
//  Copyright © 2016年 海城. All rights reserved.
//

#import "RegisteredViewController.h"

@interface RegisteredViewController ()<EMChatManagerLoginDelegate>

@property (nonatomic,retain)UITextField *userNameTF;
@property (nonatomic,retain)UITextField *pwdTF;
@property (nonatomic,retain)UIActivityIndicatorView *activityIndicatorView;     //加载过程中的动画
@end

@implementation RegisteredViewController

- (void)viewDidLoad {

    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"注 册";
    [self createLoginView];
}
#pragma mark -- 懒加载
-(UITextField *)userNameTF{
    
    if (!_userNameTF) {
        _userNameTF = [[UITextField alloc]initWithFrame:CGRectMake(160, 100, 200, 40)];
    }
    return _userNameTF;
}
-(UITextField *)pwdTF{
    
    if (!_pwdTF) {
        _pwdTF = [[UITextField alloc]initWithFrame:CGRectMake(160, 160, 200, 40)];
    }
    return _pwdTF;
}
-(UIActivityIndicatorView *)activityIndicatorView{
    
    if (!_activityIndicatorView) {
        _activityIndicatorView = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
        _activityIndicatorView.center = self.view.center;
        _activityIndicatorView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
        [self.view addSubview:_activityIndicatorView];
    }
    return _activityIndicatorView;
}

#pragma mark -- 创建注册界面
-(void)createLoginView{
    
    UILabel *userLabel = [[UILabel alloc]initWithFrame:CGRectMake(70, 100, 60, 30)];
    UILabel *pwdLabel = [[UILabel alloc]initWithFrame:CGRectMake(70, 170, 60, 30)];
    userLabel.text = @"用户名:";
    pwdLabel.text = @"密  码:";
    [self.view addSubview:userLabel];
    [self.view addSubview:pwdLabel];
    
    self.userNameTF.placeholder = @"请输入用户名";
    self.userNameTF.borderStyle = UITextBorderStyleBezel;
    [self.view addSubview:self.userNameTF];
    
    self.pwdTF.placeholder = @"请输入用户名";
    self.pwdTF.borderStyle = UITextBorderStyleBezel;
    [self.view addSubview:self.pwdTF];
    
    UIButton *registeredBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [registeredBtn setTitle:@"注册" forState:UIControlStateNormal];
    [registeredBtn setFrame:CGRectMake(180, 300,50, 40)];
    [registeredBtn addTarget:self action:@selector(registeredAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:registeredBtn];
    
}

#pragma mark -- 按钮的回调方法
-(void)registeredAction{
    //判断是否为空
    if (![self userNameOrPwd]) {   //用户名或者密码为空
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"友情提示" message:@"账号或密码不能为空" preferredStyle:UIAlertControllerStyleActionSheet];
        
        //初始化一个按钮
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
        
        [alertController addAction:action];
        
        [self presentViewController:alertController animated:YES completion:nil];
        return;
    }
    //开启动画
    [self.activityIndicatorView startAnimating];
    
    //弹出提醒
    dispatch_async(dispatch_get_main_queue(), ^{
        
        [[EaseMob sharedInstance].chatManager asyncRegisterNewAccount:self.userNameTF.text password:self.pwdTF.text withCompletion:^(NSString *username, NSString *password, EMError *error) {
            
            if (!error) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"友情提示" message:@"账号注册成功" preferredStyle:UIAlertControllerStyleActionSheet];
                    
                    //初始化一个按钮
                    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
                    
                    
                    [alertController addAction:action];
                    
                    [self presentViewController:alertController animated:YES completion:nil];
                    
                });
            }else{
                NSLog(@"注册失败  --- %@",error);
                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"友情提示" message:@"账号注册失败" preferredStyle:UIAlertControllerStyleActionSheet];
                    
                    //初始化一个按钮
                    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
                    
                    
                    [alertController addAction:action];
                    
                    [self presentViewController:alertController animated:YES completion:nil];
                    
                    
                });
            }
            
        }onQueue:nil];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            //弹出提醒消失
            [self.activityIndicatorView stopAnimating];
            //动画移除
            [self.activityIndicatorView removeFromSuperview];
        });
    });

}



//判断用户名和密码是否为空
-(BOOL)userNameOrPwd{
    
    if (self.userNameTF.text.length == 0) {
        NSLog(@"用户名为空");
        return NO;
    }else if(self.pwdTF.text.length == 0){
        NSLog(@"密码为空");
        return NO;
    }
    return YES;
}








- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
